package com.canbot.u05.activity.shoppingGuide.merchandiseShoppingGuide;


import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.activity.customvoice.ChooseAudioActivity;
import com.canbot.u05.activity.shoppingGuide.ShoppingChooseAudioActivity;
import com.canbot.u05.activity.shoppingGuide.sales.TextToSpeechActivity;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.SPUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.view.RouteRecordDialog;
import com.canbot.u05.view.UsherModeRecordDialog;

import java.io.File;
import java.util.HashMap;

/**
 * 商品内容类型选择界面
 */
public class SelectAudioTypeActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private final static String TAG = "SelectAudioTypeActivity";

        private static final int REQUEST_AUDIO = 101;

        private static final int REQUEST_TEXT_TO_SPEECH = 102;

        private static final int REQUEST_RECORD = 103;

        private RelativeLayout rlLocalSpeech;

        private RelativeLayout rlTextToSpeech;

        private RelativeLayout rlRecord;

        //    private Merchandise merchandise;
        private int editType;

        private String editpath;

        private boolean isEdit;

        private RouteRecordDialog usherModeRecordDialog;

        private String fileName;

        private int type;

        private String path;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_select_auio_type);

                isEdit = getIntent().getBooleanExtra("isExit", false);
                editType = getIntent().getIntExtra("type", -1);
                editpath = getIntent().getStringExtra("path");

                initView();


        }

        private void initView() {
                rlLocalSpeech = (RelativeLayout) findViewById(R.id.rl_local_speech);
                rlTextToSpeech = (RelativeLayout) findViewById(R.id.rl_text_to_speech);
                rlRecord = (RelativeLayout) findViewById(R.id.rl_record);

                rlLocalSpeech.setOnClickListener(this);
                rlTextToSpeech.setOnClickListener(this);
                rlRecord.setOnClickListener(this);

                if (isEdit) {
                        switch (editType) {
                                case 0:
                                        rlLocalSpeech.setSelected(true);
                                        break;
                                case 1:
                                        rlTextToSpeech.setSelected(true);
                                        break;
                                case 2:
                                        rlRecord.setSelected(true);
                                        break;
                        }
                }
        }

        /**
         * 返回
         *
         * @param view
         */
        public void back(View view) {
                setResult(RESULT_CANCELED);
                finish();
                overridePendingTransition(R.anim.face_zoom_enter,
                        R.anim.face_zoom_exit);
        }

        /**
         * 选择完语音后提交
         *
         * @param view
         */
        public void complete(View view) {
                if (TextUtils.isEmpty(path)){
                        ToastTools.newInstance(this).showText(getString(R.string.voice_cannot_empty));
                        return;
                }
                Intent intent = new Intent();
                intent.putExtra("type", type);
                intent.putExtra("path", path);
                setResult(RESULT_OK, intent);
                finish();
                overridePendingTransition(R.anim.face_zoom_enter,
                        R.anim.face_zoom_exit);
        }



        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.rl_local_speech:
                                Intent intent = new Intent(SelectAudioTypeActivity.this, ChooseAudioActivity.class);
                                if (isEdit) {
                                        if (editType == 0){
                                                intent.putExtra("voice", editpath);
                                        }

                                }
                                intent.putExtra("path", "/uusound");
                                startActivityForResult(intent, REQUEST_AUDIO);
                                break;
                        case R.id.rl_text_to_speech:
                                //文字转语音
                                Intent intent1 = new Intent(SelectAudioTypeActivity.this, TextToSpeechActivity.class);
                                if (isEdit) {
                                        if (editType == 1){
                                                intent1.putExtra("voice", editpath);
                                        }
                                }
                                startActivityForResult(intent1, REQUEST_TEXT_TO_SPEECH);

                                break;
                        case R.id.rl_record:
                                //录音
                                record();

                                break;
                        case R.id.record_dialog_finish:
                                fileName = usherModeRecordDialog.ok();
                                Logger.e("test", "fileName" + fileName);
                                usherModeRecordDialog.setTime(SelectAudioTypeActivity.this.getString(R.string.uploading_please_wait));
                                Logger.d("SpeechSetupActivity", "录音的文件名：" + fileName);
                                uploadFile();

                                break;
                        case R.id.record_dialog_cancle:
                                usherModeRecordDialog.no();
                                //finish();
                                usherModeRecordDialog.dismiss();
                                Logger.d("SpeechSetupActivity", "录音的文件名：" + fileName);
                                break;
                        default:
                                break;
                }

                overridePendingTransition(R.anim.face_zoom_enter,
                        R.anim.face_zoom_exit);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {

                if (resultCode == RESULT_CANCELED){
                        return;
                }
                if (requestCode == REQUEST_AUDIO ) {
                        boolean isExist = data.getBooleanExtra("isChecked", false);
                        String path = data.getStringExtra("path");  //path为全路径
                        Logger.e("SelectAnswerTypeActivity", "选中的语音路径 ：" + path);
                        rlLocalSpeech.setSelected(true);
                        rlRecord.setSelected(false);
                        rlTextToSpeech.setSelected(false);
                        type = 0;
                        this.path = path;
                }
                else if (requestCode == REQUEST_TEXT_TO_SPEECH ) {
                        boolean isExist = data.getBooleanExtra("isChecked", false);
                        String content = data.getStringExtra("ttsResult");
                        Logger.e("SelectAnswerTypeActivity", "输入回答为 ：" + content);
                        type = 1;
                        this.path = content;
                        rlLocalSpeech.setSelected(false);
                        rlRecord.setSelected(false);
                        rlTextToSpeech.setSelected(true);

                }
        }

        /**
         * 进入录音
         */
        private void record() {
                if (usherModeRecordDialog == null) {
                        //点击语音录入图标
                        usherModeRecordDialog = new RouteRecordDialog(this, RouteRecordDialog.USHER_MODE);
                        usherModeRecordDialog.setFinishClickListener(this);
                        usherModeRecordDialog.setCancleClickListener(this);
                        setDialogWindowAttr(usherModeRecordDialog, this);
                        usherModeRecordDialog.show();
                }
                else {
                        usherModeRecordDialog.show();
                }

        }

        //上传到机器人头部的某个文件夹下 对应头部 RecordVoiceServlet
        private String url = IpMsgConst.HTTP_URL + "uploadRecord";

        private void uploadFile() {
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("audiofile", new File(UsherModeRecordDialog.RECORD_FILE_DIR, fileName));
                hashMap.put("path", fileName);
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                Logger.e("test", result + "上传成功");

                                SPUtils.saveBoolean(SelectAudioTypeActivity.this, "isTTS", false);
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                usherModeRecordDialog.cancel();
                                                ToastTools.newInstance(SelectAudioTypeActivity.this).showText(getString(R.string.upload_complete));
                                                String path = fileName;
                                                Logger.e("SelectAnswerTypeActivity", "录音文件路径 ：" + path);
                                                rlLocalSpeech.setSelected(false);
                                                rlRecord.setSelected(true);
                                                rlTextToSpeech.setSelected(false);
                                                type = 2;
                                                SelectAudioTypeActivity.this.path = PathConst.RECORD_FILE_PATH + path;
                                        }
                                });


                        }

                        @Override
                        public void onFailure() {

                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                usherModeRecordDialog.cancel();
                                                ToastTools.newInstance(SelectAudioTypeActivity.this).showText(getString(R.string.upload_failed));
                                        }
                                });
                        }
                });
        }

        public static void setDialogWindowAttr(Dialog dlg, Context ctx) {
                dlg.setCancelable(false);
                Window window = dlg.getWindow();
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.gravity = Gravity.CENTER;
                lp.width = (int) ctx.getResources().getDimension(R.dimen.x536);//宽高可设置具体大小
                lp.height = (int) ctx.getResources().getDimension(R.dimen.x436);
                dlg.getWindow().setAttributes(lp);
        }

        @Override
        protected void onDestroy() {
                isEdit = false;
                super.onDestroy();
        }
}
